package com.yky.utils.base;

import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.log4j.Logger;

/**
 * 
 * 工具类
 * 
 * 文件功能简述： 描述一个URL或图片地址是否有效
 * 
 */
@SuppressWarnings("unused")
public class URLAvailability {

	private static URL urlStr;
	private static HttpURLConnection connection;
	private static int state = -1;

	/**
	 * 功能描述 : 检测当前URL是否可连接或是否有效, 最多连接网络 5 次, 如果 5 次都不成功说明该地址不存在或视为无效地址.
	 * 
	 * @param url
	 *            指定URL网络地址
	 * @return String
	 */
	public static boolean isConnect(String url) {
		int counts = 0;
		boolean succ = false;
		if (url == null || url.length() <= 0) {
			return succ;
		}
		while (counts < 5) {
			try {
				urlStr = new URL(url);
				connection = (HttpURLConnection) urlStr.openConnection();
				state = connection.getResponseCode();
				if (state == 200) {
					succ = true;
				}
				break;
			} catch (Exception ex) {
				counts++;
				continue;
			}
		}
		return succ;
	}

	public static void main(String[] args) {
		boolean s = isConnect("http://10.2.83.208:8080/midcdc/uploadFiles/images/20101217173111-6a8f0dc4-f532-47b1-a498-55c7deff75b2.jpg");
		System.out.println(s);
	}
}
